﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using Avaxo.Scripting;

namespace Avaxo.DlrPad
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow
    {
        private readonly List<string> xamlVariables = new List<string>();

        public MainWindow()
        {
            InitializeComponent();

            Binding fontSizeBinding = new Binding( "Text" );
            fontSizeBinding.Source = fontSizeCombo;
            console.console.SetBinding( FontSizeProperty, fontSizeBinding );
        }

        private static void ForEachFrameworkElement( object root, Action<FrameworkElement> action )
        {
            FrameworkElement element = root as FrameworkElement;
            if ( element != null )
            {
                action( element );
            }

            if ( root is DependencyObject )
            {
                foreach ( object child in LogicalTreeHelper.GetChildren( (DependencyObject) root ) )
                {
                    ForEachFrameworkElement( child, action );
                }
            }
        }

        private void OnFrameContentRendered( object sender, EventArgs e )
        {
            ClearXamlVariables();
            AddXamlVariablesRecursive();
        }

        private void OnXamlParsed( object sender, XamlParsedEventArgs e )
        {
            contentFrame.Content = e.ParsedXamlObject;
            SetStatusText( "Done." );
        }

        private void OnXamlParseError( object sender, ErrorEventArgs e )
        {
            SetStatusText( "Error: " + e.Exception.Message );
        }

        private void SetStatusText( string statusText )
        {
            string content = string.Empty;
            if ( !string.IsNullOrEmpty( statusText ) )
            {
                content = statusText.Replace( Environment.NewLine, " " );
            }
            infoStatusItem.Content = content;
        }

        private void ClearXamlVariables()
        {
            while ( xamlVariables.Count > 0 )
            {
                console.console.LanguageEngine.CurrentModule.RemoveVariable( xamlVariables[ 0 ] );
                xamlVariables.RemoveAt( 0 );
            }
        }

        private void AddXamlVariablesRecursive()
        {
            Action<FrameworkElement> add = delegate( FrameworkElement element )
                                           {
                                               if ( !string.IsNullOrEmpty( element.Name ) )
                                               {
                                                   console.console.LanguageEngine.CurrentModule.
                                                       SetVariable(
                                                       element.Name, element );
                                                   xamlVariables.Add( element.Name );
                                               }
                                           };
            ForEachFrameworkElement( contentFrame.Content, add );
        }

        private void OnLanguageChanged( object sender, SelectionChangedEventArgs e )
        {
            if ( languagesCombo.SelectedItem != null )
            {
                console.console.LanguageEngine = languagesCombo.SelectedItem as ILanguageEngine;
            }
        }
    }
}